package com.example.shopapp;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.fragment.CartFragment;
import com.example.fragment.CatalogFragment;
import com.example.fragment.HomeFragment;
import com.example.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    FrameLayout frameLayout;
    HomeFragment homeFragment;
    CartFragment cartFragment;
    CatalogFragment catalogFragment;
    MineFragment mineFragment;
    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frameLayout = findViewById(R.id.main_content);

        //导航栏
        bottomNavigationView = findViewById(R.id.nav_menu_bar);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item){
                if (item.getItemId() == R.id.menu_home){
                    SelectItemBar(0);
                    updateIconState(item, R.drawable.first_select);
                    return true;
                }
                if (item.getItemId() == R.id.menu_catalog){
                    SelectItemBar(1);
                    updateIconState(item, R.drawable.category_select);
                    return true;
                }
                if (item.getItemId() == R.id.menu_cart){
                    SelectItemBar(2);
                    updateIconState(item, R.drawable.cart_select);
                    return true;
                }
                if (item.getItemId() == R.id.menu_mine){
                    SelectItemBar(3);
                    updateIconState(item, R.drawable.mine_select);
                    return true;
                }
                return false;
            }
        });

        SelectItemBar(0);
    }

    /**
     * 更新其他菜单项的图标为未选中状态
     */
    private void updateIconState(MenuItem selectedItem, int selectedIconRes) {
        MenuItem homeItem = bottomNavigationView.getMenu().findItem(R.id.menu_home);
        MenuItem catalogItem = bottomNavigationView.getMenu().findItem(R.id.menu_catalog);
        MenuItem cartItem = bottomNavigationView.getMenu().findItem(R.id.menu_cart);
        MenuItem mineItem = bottomNavigationView.getMenu().findItem(R.id.menu_mine);

        // 重置所有图标为未选中状态
        if (homeItem != selectedItem) homeItem.setIcon(R.drawable.first);
        if (catalogItem != selectedItem) catalogItem.setIcon(R.drawable.category);
        if (cartItem != selectedItem) cartItem.setIcon(R.drawable.cart);
        if (mineItem != selectedItem) mineItem.setIcon(R.drawable.mine);

        // 设置当前选中项的图标
        selectedItem.setIcon(selectedIconRes);
    }

    //排他思想
    public void hideFragment(FragmentTransaction fragmentTransaction){
        if (homeFragment != null){
            fragmentTransaction.hide(homeFragment);
        }
        if (cartFragment != null){
            fragmentTransaction.hide(cartFragment);
        }
        if (catalogFragment != null){
            fragmentTransaction.hide(catalogFragment);
        }
        if (mineFragment != null){
            fragmentTransaction.hide(mineFragment);
        }
    }

    public void SelectItemBar(int index){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.main_content, homeFragment);
                } else {
                    fragmentTransaction.show(homeFragment);
                }
                break;
            case 1:
                if (catalogFragment == null) {
                    catalogFragment = new CatalogFragment();
                    fragmentTransaction.add(R.id.main_content, catalogFragment);
                } else {
                    fragmentTransaction.show(catalogFragment);
                }
                break;
            case 2:
                if (cartFragment == null) {
                    cartFragment = new CartFragment();
                    fragmentTransaction.add(R.id.main_content, cartFragment);
                } else {
                    fragmentTransaction.show(cartFragment);
                }
                break;
            case 3:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.main_content, mineFragment);
                } else {
                    fragmentTransaction.show(mineFragment);
                }
                break;
        }

        fragmentTransaction.commit();
    }





}